// ///////////////////////////// MIT License //////////////////////////////////// //
//                                                                                //
// Copyright (c) 2010 David Zsolt Manrique                                        //
//                    david.zsolt.manrique@gmail.com                              //
//                                                                                //
// Permission is hereby granted, free of charge, to any person obtaining a copy   //
// of this software and associated documentation files (the "Software"), to deal  //
// in the Software without restriction, including without limitation the rights   //
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell      //
// copies of the Software, and to permit persons to whom the Software is          //
// furnished to do so, subject to the following conditions:                       //
//                                                                                //
// The above copyright notice and this permission notice shall be included in     //
// all copies or substantial portions of the Software.                            //
//                                                                                //
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR     //
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,       //
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE    //
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER         //
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,  //
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN      //
// THE SOFTWARE.                                                                  //
//                                                                                //
// ////////////////////////////////////////////////////////////////////////////// //


#include "integrate.h"


bool operator >>= (const std::vector<std::string> & input_lines,velocity_verlet_control & get)
{
	std::vector<std::string> block;
    io::parse_block("<velocity-verlet>","<\\velocity-verlet>",input_lines,block);
	get.DT = 0.1;
	get.dt = 0.001;
	get.t0 = 0.0;
	get.t1 = 10.0;
    for(std::vector<std::string>::iterator line = block.begin(); line != block.end(); line++)
	if(io::match("t0 %1",*line,&get.t0)) ;
	else if (io::match("t1 %1",*line,&get.t1)) ;
    else if (io::match("dt %1",*line,&get.dt)) ;
    else if (io::match("DT %1",*line,&get.DT)) ;
    else throw std::runtime_error("Syntax error: "+*line);
	if(block.size() == 0) return false;
	return true;
}
std::ostream & operator << (std::ostream & os,const velocity_verlet & get)
{
	os << "<velocity-verlet-report>" << std::endl;
	os << "Number of atoms : " << get.n << std::endl;
	os << "<\\velocity-verlet-report>" << std::endl;
	return os;
}


bool operator >>= (const std::vector<std::string> & input_lines,steepest_descent_control & get)
{
	std::vector<std::string> block;
    io::parse_block("<steepest-descent>","<\\steepest-descent>",input_lines,block);
	get.kappa = 1e-3;
	get.prec = 1.0e-5;
    get.kmax = 1000;
	get.every = 10;
    for(std::vector<std::string>::iterator line = block.begin(); line != block.end(); line++)
	if(io::match("kappa %1",*line,&get.kappa)) ;
	else if (io::match("prec %1",*line,&get.prec)) ;
    else if (io::match("max_it %1",*line,&get.kmax)) ;
	else if (io::match("every %1",*line,&get.every)) ;
    else throw std::runtime_error("Syntax error: "+*line);
	if(block.size() == 0) return false;
	return true;
}
std::ostream & operator << (std::ostream & os,const steepest_descent & get)
{
	os << "<steepest-descent-report>" << std::endl;
	os << "Number of atoms : " << get.n << std::endl;
	os << "Number of iteration : " << get.k << std::endl;
	if(get.k >= get.kmax) 
		os << " >>> Maximum "<< get.kmax << " iteration achieved! <<< " << std::endl; 
	os << "Potential energy : " << get.u << std::endl;
	os << "<\\steepest-descent-report>" << std::endl;
	return os;
}

